package frame;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import otherSAMS.jdbc.DBconnection;

public class RegistView extends JFrame {
    public RegistView() {
        // Registration interface window settings
        this.setTitle("注册界面");
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        this.setSize(450, 350);
        this.setLocationRelativeTo(null);
        this.setLayout(null);
        this.setVisible(true);

        JLabel titleLabel = new JLabel("用户注册");
        titleLabel.setBounds(170, 15, 200, 30);
        titleLabel.setFont(new Font("黑体", Font.BOLD, 24));
        this.add(titleLabel);

        JLabel account = new JLabel("注册账户：");
        account.setBounds(45, 60, 100, 30);
        account.setFont(new Font("宋体", Font.BOLD, 18));
        this.add(account);

        JLabel password = new JLabel("注册密码：");
        password.setBounds(45, 110, 100, 30);
        password.setFont(new Font("宋体", Font.BOLD, 18));
        this.add(password);

        JLabel nextpassword = new JLabel("再次输入密码：");
        nextpassword.setBounds(35, 160, 150, 30);
        nextpassword.setFont(new Font("宋体", Font.BOLD, 18));
        this.add(nextpassword);

        JLabel roleLabel = new JLabel("用户类型：");
        roleLabel.setBounds(50, 210, 100, 30);
        roleLabel.setFont(new Font("宋体", Font.BOLD, 18));
        this.add(roleLabel);

        JTextField account_ = new JTextField(30);
        JPasswordField password_ = new JPasswordField(30);
        JPasswordField nextpassword_ = new JPasswordField(30);
        String[] roles = {"学生", "教师"};
        JComboBox<String> roleComboBox = new JComboBox<>(roles);

        account_.setBounds(160, 60, 250, 30);
        password_.setBounds(160, 110, 250, 30);
        nextpassword_.setBounds(160, 160, 250, 30);
        roleComboBox.setBounds(160, 210, 250, 30);

        this.add(account_);
        this.add(password_);
        this.add(nextpassword_);
        this.add(roleComboBox);

        JButton registerButton = new JButton("注册");
        registerButton.setBounds(110, 260, 80, 40);
        this.add(registerButton);

        JButton exitButton = new JButton("退出");
        exitButton.setBounds(260, 260, 80, 40);
        this.add(exitButton);

        ImageIcon icon=new ImageIcon("src/main/java/jieke1/1.png");
        JLabel label=new JLabel(icon);
        label.setBounds(0,0,430,310);
        this.add(label);

        registerButton.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                String username = account_.getText();
                String password = new String(password_.getPassword());
                String confirmPassword = new String(nextpassword_.getPassword());
                String role = (String) roleComboBox.getSelectedItem();

                if (username.isEmpty() || password.isEmpty() || confirmPassword.isEmpty() || role.isEmpty()) {
                    JOptionPane.showMessageDialog(null, "所有字段都是必填的！");
                    return;
                }

                if (!password.equals(confirmPassword)) {
                    JOptionPane.showMessageDialog(null, "两次输入的密码不一致！");
                    return;
                }

                if (accountExists(username, role)) {
                    JOptionPane.showMessageDialog(null, "该账户已存在！");
                } else {
                    registerAccount(username, password, role);
                }
            }
        });

        exitButton.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                dispose();
            }
        });
    }

    private boolean accountExists(String username, String role) {
        String table = role.equals("学生") ? "user" : "user1";
        try (Connection connection = DBconnection.getConnection()) {
            String query = "SELECT COUNT(*) FROM " + table + " WHERE username = ?";
            PreparedStatement statement = connection.prepareStatement(query);
            statement.setString(1, username);
            ResultSet resultSet = statement.executeQuery();

            if (resultSet.next()) {
                int count = resultSet.getInt(1);
                return count > 0;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    private void registerAccount(String username, String password, String role) {
        String table = role.equals("学生") ? "user" : "user1";
        try (Connection connection = DBconnection.getConnection()) {
            String query = "INSERT INTO " + table + " (username, password) VALUES (?, ?)";
            PreparedStatement statement = connection.prepareStatement(query);
            statement.setString(1, username);
            statement.setString(2, password);

            int rowsInserted = statement.executeUpdate();
            if (rowsInserted > 0) {
                JOptionPane.showMessageDialog(null, "注册成功！");
                dispose(); // Close the registration window
            } else {
                JOptionPane.showMessageDialog(null, "注册失败！");
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(RegistView::new);
    }
}
